package com.cszjzx.member.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cszjzx.member.po.Department;
import com.cszjzx.member.po.Master;
import com.cszjzx.member.service.DepartmentService;
import com.cszjzx.member.service.DepartmentServiceImpl;
import com.cszjzx.member.service.MasterService;
import com.cszjzx.member.service.MasterServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ManagementOfAdminAction extends ActionSupport {

	
	/**
	 * 绠＄悊鍛樼鐞嗛噷闈㈠垹闄ょ鐞嗗憳鍙彁渚涘崟涓垹闄ょ殑鍔熻兘
	 */
	private String masterId;
	private String password;
	private String message;
	private List<Master> masters;
	private List<Department> departments;
	private int departmentid;
	MasterService masterservice = new MasterServiceImpl();
	DepartmentService departmentservice = new DepartmentServiceImpl();
	HttpServletRequest request = ServletActionContext.getRequest();
	Master master = (Master)request.getSession().getAttribute("master");
	

	
	
	public List<Department> getDepartments() {
		return departments;
	}
	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}
	public int getDepartmentid() {
		return departmentid;
	}
	public void setDepartmentid(int departmentid) {
		this.departmentid = departmentid;
	}
	public List<Master> getMasters() {
		return masters;
	}
	public void setMasters(List<Master> masters) {
		this.masters = masters;
	}

	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	public String getMasterId() {
		return masterId;
	}
	public void setMasterId(String masterId) {
		this.masterId = masterId;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	  
	

	/**
	 * 鑾峰彇鏁版嵁锛氭棤
	 * 浣滅敤锛氳幏鍙栦竴涓墍鏈夌鐞嗗憳鐨勫垪琛�
	 * 杞嚭锛歮anagementOfAdmin.jsp
	 * 鎻愪緵鏁版嵁锛氱鐞嗗憳鍒楄〃
	 * @return
	 * @throws Exception
	 */
	public String show() throws Exception{

		masters = masterservice.getMasters(master.getModule());
		return "showAdmin";
	}
	
	public String departments() throws Exception{
		
		departments = departmentservice.getDepartments();
		return "departments";
	}
	/**
	 * 涓�涓鐞嗗憳瀵硅薄
	 * 娣诲姞涓�涓鐞嗗憳
	 * 娣诲姞鎴愬姛纭鐣岄潰
	 * 鏃�
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception{
		
		if(masterId.equals("")||password.equals("")){
			message="鏁版嵁鏈緭鍏ワ紝璇疯緭鍏ユ暟鎹紒";
		}else{
			if(masterservice.getMasterById(masterId, master.getModule())!=null){
				message="该管理员已经存在";
			}else{
				if(masterservice.addMaster(masterId, password,master.getModule(),departmentid)==true){
					message = "娣诲姞绠＄悊鍛樻垚鍔�";
				}else{
					message = "娣诲姞绠＄悊鍛樺け璐�";
				}
			}
		}
		return "addAdmin";
	}
	
	/**
	 * 涓�涓鐞嗗憳鐨刬d
	 * 鍒犻櫎杩欎簺绠＄悊鍛�
	 * 鍒犻櫎鎴愬姛纭鐣岄潰
	 * 鏃�
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception{
		
		if(masterservice.deleteMaster(masterId)==true){
			message = "鍒犻櫎绠＄悊鍛樻垚鍔�";
		}else{
			message = "鍒犻櫎绠＄悊鍛樺け璐�";
		}
		
		return "deleteAdmin";
	}
}
